/**
 * Copyright (c) 时间, Tal.Ben 袁东昊 (ydonghao2@gmail.com).
 *
 * 版权
 */
package com.talben.soudian.ui;

import org.apache.http.Header;
import org.json.JSONObject;

import android.content.Intent;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import butterknife.InjectView;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.talben.soudian.AppContext;
import com.talben.soudian.AppManager;
import com.talben.soudian.R;
import com.talben.soudian.TDevice;
import com.talben.soudian.api.SouDianAPI;
import com.talben.soudian.base.BaseActivity;
import com.talben.soudian.util.SimpleTextWatcher;
import com.talben.soudian.util.StringUtils;

/**
 * @Title: ChangePasswordActivity.java
 * @Package com.talben.soudian.ui
 * @Description: 修改密码
 * @author Tal.Ben
 * @date 2015年5月30日 下午1:22:01
 * @version v1.0
 */
public class ChangePasswordActivity extends BaseActivity{

	@InjectView(R.id.et_old_password)
	EditText mEtOldPassword;
	@InjectView(R.id.iv_clear_old_password)
	ImageView mIvClearOldPassword;
	@InjectView(R.id.et_new_password)
	EditText mEtNewPassword;
	@InjectView(R.id.iv_new_clear_password)
	ImageView mIvClearNewPassword;
	@InjectView(R.id.et_confirm_password)
	EditText mEtConfirmPassword;
	@InjectView(R.id.iv_confirm_clear_password)
	ImageView mIvClearConfirmPassword;
	@InjectView(R.id.btn_change_password)
	Button mBtnChangePassword;
	
	private String mOldPassword;
	private String mNewPassword;
	private String mConfirmPassword;
	
	@Override
    protected int getLayoutId() {
        return R.layout.activity_change_password;
    }
    
    @Override
    protected boolean hasBackButton() {
        return true;
    }
    
    @Override
    protected int getActionBarTitle() {
        return R.string.user_password;
    }
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
        case R.id.iv_clear_old_password:
        	mEtOldPassword.getText().clear();
        	mEtOldPassword.requestFocus();
            break;
        case R.id.iv_new_clear_password:
        	mEtNewPassword.getText().clear();
        	mEtNewPassword.requestFocus();
            break;
        case R.id.iv_confirm_clear_password:
        	mEtConfirmPassword.getText().clear();
        	mEtConfirmPassword.requestFocus();
            break;
        case R.id.btn_change_password:
            handleChangePassword();
            break;
        default:
            break;
        }
	}
	

	private void handleChangePassword() {
		if (!prepareForChangePassword()) {
            return;
        }
		
		// if the data has ready
		mOldPassword = mEtOldPassword.getText().toString();
		mNewPassword = mEtNewPassword.getText().toString();
		mConfirmPassword = mEtConfirmPassword.getText().toString();
		
		showWaitDialog(R.string.progress_change_passwod);
		SouDianAPI.changePwd(mOldPassword , mNewPassword , mHandler);
	}
	
	private final AsyncHttpResponseHandler mHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {
			hideWaitDialog();
			AppContext.showToast(R.string.tip_illegal_change_password);
		}

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
			// 处理成功后的动作
			try {
				//处理Json
				String result = new String(arg2,"utf-8");
				if (result.contains("success")) {
					AppContext.showToast("修改成功");
					handleChangePwdSuccess();
				} else {
					AppContext.showToast(R.string.tip_illegal_change_password);
				}
				
			} catch (Exception e) {
				e.printStackTrace();
                onFailure(arg0, arg1, arg2, e);
			}
		}
    };

	private boolean prepareForChangePassword() {
		if (!TDevice.isNetworkReady(getApplicationContext())) {
            AppContext.showToastShort(R.string.tip_no_internet);
            return false;
        }
		String oldPassword = mEtOldPassword.getText().toString();
        if (StringUtils.isEmpty(oldPassword)) {
            AppContext.showToastShort(R.string.tip_please_old_password);
            mEtOldPassword.requestFocus();
            return false;
        }
        String newPassword = mEtNewPassword.getText().toString();
        if (StringUtils.isEmpty(newPassword)) {
            AppContext.showToastShort(R.string.tip_please_new_password);
            mEtNewPassword.requestFocus();
            return false;
        }
        String confirmPassword = mEtConfirmPassword.getText().toString();
        if (StringUtils.isEmpty(confirmPassword)) {
            AppContext.showToastShort(R.string.tip_please_confirm_password);
            mEtConfirmPassword.requestFocus();
            return false;
        } else if(!newPassword.equals(confirmPassword)){
        	mEtConfirmPassword.setText("");
        	mEtConfirmPassword.requestFocus();
        	return false;
        }
		return true;
	}

	protected void handleChangePwdSuccess() {
		AppManager.getAppManager().finishActivity(ChangeUserActivity.class);
		startActivity(new Intent(this, ChangeUserActivity.class));
		finish();
	}

	@Override
	public void initView() {
		mIvClearOldPassword.setOnClickListener(this);
		mIvClearNewPassword.setOnClickListener(this);
		mIvClearConfirmPassword.setOnClickListener(this);
		
		mBtnChangePassword.setOnClickListener(this);
		
		mEtOldPassword.addTextChangedListener(mOldPasswordWatcher);
		mEtNewPassword.addTextChangedListener(mNewPasswordWatcher);
		mEtConfirmPassword.addTextChangedListener(mConfirmPasswordWatcher);
	}
	
	private final TextWatcher mOldPasswordWatcher = new SimpleTextWatcher(){
		@Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
			mIvClearOldPassword
                    .setVisibility(TextUtils.isEmpty(s) ? View.INVISIBLE
                            : View.VISIBLE);
        }
	};
	public final TextWatcher mNewPasswordWatcher = new SimpleTextWatcher(){
		@Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
			mIvClearNewPassword
                    .setVisibility(TextUtils.isEmpty(s) ? View.INVISIBLE
                            : View.VISIBLE);
        }
	};
	public final TextWatcher mConfirmPasswordWatcher = new SimpleTextWatcher(){
		@Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
			mIvClearConfirmPassword
                    .setVisibility(TextUtils.isEmpty(s) ? View.INVISIBLE
                            : View.VISIBLE);
        }
	};

	@Override
	public void initData() {
		// TODO Auto-generated method stub
		
	}

}

